package tongwentongshu.com.app.activity;

import android.support.v4.util.ArrayMap;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import butterknife.BindView;
import butterknife.OnClick;
import tongwentongshu.com.app.R;
import tongwentongshu.com.app.bean.DouBean;
import tongwentongshu.com.app.bean.WithBean;
import tongwentongshu.com.app.bean.WithInfoBean;
import tongwentongshu.com.app.contract.CallBackColonyContract;
import tongwentongshu.com.app.db.Cache;
import tongwentongshu.com.app.dialog.AlertUtils;
import tongwentongshu.com.app.network.ResponseSubscriber;
import tongwentongshu.com.app.update.UpdateFractory;
import tongwentongshu.com.app.utils.ToastUtil;

/**
 * Created by XiongBin on 2017/7/18.
 */

public class ShudouWithdrawalsActivity extends BaseActivity {

    @BindView(R.id.tv_shudou_sum)
    TextView doudou;//书豆
    @BindView(R.id.tv_allow_shudou)
    TextView allowShudou;//本次可提取书豆数
    @BindView(R.id.tv_limit_shudou)
    TextView limitShudou;//书豆上限
    @BindView(R.id.tv_name)
    TextView name;//真实姓名
    @BindView(R.id.tv_money)
    TextView money;//折算钱数
    @BindView(R.id.tv_bank)
    TextView bank;//银行
    @BindView(R.id.tv_bank_no)
    EditText bankNo;//银行卡号
    @BindView(R.id.tv_shudou_no)
    EditText shudouNo;//提取书豆数量
    @BindView(R.id.btn_ok)
    Button withdrawals;//提取


    private Map<String, String> map;

    @Override
    protected int getLayoutResourceId() {
        return R.layout.activity_withdrawals_shudou;
    }

    @Override
    protected int getTitleId() {
        return R.string.withdrawals_shudou;
    }

    @Override
    protected void initView() {
        bankNo.setInputType(InputType.TYPE_CLASS_NUMBER
                | InputType.TYPE_NUMBER_FLAG_DECIMAL);
        shudouNo.setInputType(InputType.TYPE_CLASS_NUMBER
                | InputType.TYPE_NUMBER_FLAG_DECIMAL);
        setPricePoint(shudouNo);
    }

    @Override
    protected void initData() {
        initInfo();
    }


    public void setPricePoint(final EditText editText) {
        editText.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
                if (TextUtils.isEmpty(s)) {
                    return;
                }
                Pattern p = Pattern.compile("[0-9]*");
                Matcher m = p.matcher(s);
                if (!m.matches()) {
                    return;
                }
                Double money_ = Double.valueOf(shudouNo.getText().toString().trim()) / 10;
                money.setText(money_ + "");
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub

            }

        });

    }

    int currentindex = 0;

    @OnClick({R.id.btn_ok, R.id.tv_bank})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_ok:
                withdrawals();
                break;
            case R.id.tv_bank:
                AlertUtils.initWheelDialog(mContext, list, currentindex, new CallBackColonyContract.OnWheelBack() {
                    @Override
                    public void wheelCallBack(int index) {
                        currentindex = index;
                        bank.setText(list[index]);
                        AlertUtils.dismiss();
                    }
                });
                break;
        }
    }

    String[] list;

    public void initInfo() {
        map = new ArrayMap<>();
        map.put("token", Cache.getToken());

        UpdateFractory.getBuild()
                .name("WithdrawalsInfoCall")
                .map(map)
                .build()
                .execute(new ResponseSubscriber<WithInfoBean>() {

                    @Override
                    public void onFailure(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onSuccess(WithInfoBean bean, String msg) {
                        list = new String[bean.getData().getBanklist().size()];
                        for (int i = 0; i < bean.getData().getBanklist().size(); i++) {
                            list[i] = bean.getData().getBanklist().get(i);
                        }
                        if (list.length > 0) {
                            bank.setText(list[currentindex]);
                        }
                        name.setText(bean.getData().getUser_name());
                        doudou.setText(bean.getData().getDoudou() + "书豆");
                        allowShudou.setText(bean.getData().getSurplus_doudou() + "书豆");
                        limitShudou.setText(bean.getData().getDoudou_max_num() + "书豆");

                    }

                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                    }
                });
    }

    public void withdrawals() {
        map = new ArrayMap<>();
        String bank_no = bankNo.getText().toString().trim();
        String doudouno = shudouNo.getText().toString().trim();
        if (TextUtils.isEmpty(bank_no)) {
            ToastUtil.show(mContext, "银行卡号不能为空");
            return;
        }
        if (TextUtils.isEmpty(doudouno)) {
            ToastUtil.show(mContext, "书豆数量不能为0");
            return;
        }
        map.put("bank_user_name", name.getText().toString().trim());
        map.put("doudou_num", doudouno);
        map.put("bank_name", list[currentindex]);
        map.put("bank_num", bank_no);
        map.put("token", Cache.getToken());

        UpdateFractory.getBuild()
                .name("WithdrawalsCall")
                .map(map)
                .build()
                .execute(new ResponseSubscriber<WithBean>() {

                    @Override
                    public void onFailure(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onSuccess(WithBean bean, String msg) {
                        ToastUtil.show(mContext, msg);
                        finish();
                    }

                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                    }
                });
    }

}
